package signGrammar.primitives;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import signGrammar.IShape;

public class CLineShape implements IShape {
	private static final int STEP = 25; 
	private static final Color DEFAULT_BRUSH_COLOR = Color.RED;
	
	public static CLineShape N(Point p) {
		return new CLineShape(0, -STEP, p);
	}

	public static CLineShape S(Point p) {
		return new CLineShape(0, STEP, p);
	}

	public static CLineShape NE(Point p) {
		return new CLineShape(STEP, -STEP, p);
	}

	public static CLineShape E(Point p) {
		return new CLineShape(STEP, 0, p);
	}

	public static CLineShape W(Point p) {
		return new CLineShape(-STEP, 0, p);
	}

	public static CLineShape SE(Point p) {
		return new CLineShape(STEP, STEP, p);
	}
	
	public static CLineShape SW(Point p) {
		return new CLineShape(-STEP, STEP, p);
	}

	public static CLineShape NW(Point p) {
		return new CLineShape(-STEP, -STEP, p);
	}
	
	public static CLineShape N() {
		return new CLineShape(0, -STEP, new Point());
	}
	
	public static CLineShape S() {
		return new CLineShape(0, STEP, new Point());
	}
	
	public static CLineShape NE() {
		return new CLineShape(STEP, -STEP, new Point());
	}
	
	public static CLineShape E() {
		return new CLineShape(STEP, 0, new Point());
	}
	
	public static CLineShape W() {
		return new CLineShape(-STEP, 0, new Point());
	}
	
	public static CLineShape SE() {
		return new CLineShape(STEP, STEP, new Point());
	}
	
	public static CLineShape SW() {
		return new CLineShape(-STEP, STEP, new Point());
	}
	
	public static CLineShape NW() {
		return new CLineShape(-STEP, -STEP, new Point());
	}

	// --------------------------------------------------------------

	
	private final Point head;
	private final Point tail;
	private final int dx, dy;

	public CLineShape(int dx, int dy, Point startPoint) {
		this.dx = dx;
		this.dy = dy;
		this.head = startPoint;
		this.tail = calculateTail(startPoint);
	}

	public CLineShape print(Graphics graphics) {
		graphics.setColor(DEFAULT_BRUSH_COLOR);
		graphics.drawLine(head.x, head.y, tail.x, tail.y);
		return this;
	}

	public Point getHead() {
		return head;
	}

	public Point getTail() {
		return tail;
	}

	@Override
	public IShape cloneForHead(Point startPoint) {
		return new CLineShape(dx, dy, startPoint);
	}

	@Override
	public IShape cloneForTail(Point endPoint) {
		return new CLineShape(dx, dy, calculateHead(endPoint) );
	}

	private Point calculateHead(Point tail) {
		return new Point(tail.x - dx, tail.y - dy);
	}

	private Point calculateTail(Point head) {
		return new Point(head.x + dx, head.y + dy);
	}
}
